package com.xxmw.transaction.service.impl;

import com.xxmw.transaction.common.excep.service.TransactionException;
import com.xxmw.transaction.common.utils.Constant;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxmw.transaction.common.entity.P;
import com.xxmw.transaction.mapper.SysDictMapper;
import com.xxmw.transaction.model.SysDict;
import com.xxmw.transaction.service.SysDictService;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service("sysDictService")
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

	@Override
	public P<SysDict> getSysDictList(int pageNum, int pageSize, String key) {
		IPage<SysDict> page = new Page<>(pageNum, pageSize);
		QueryWrapper<SysDict> query = new QueryWrapper<>();
		query.eq(StringUtils.isNotBlank(key), "`key`", key);
		page(page, query);
		return new P<>(page.getTotal(), page.getRecords());
	}

	@Override
	public SysDict getSysDict(String key) {
		QueryWrapper<SysDict> query = new QueryWrapper<>();
		query.eq("`key`", key);
		return getOne(query);
	}

	@Override
	public String getValue(String key) {
		return getSysDict(key).getValue();
	}

	@Override
	public Map<String, BigDecimal> getBourseOrderMinAmountAndFloat(Integer bourse) {
		Map<String, BigDecimal> map = new HashMap<>();
		String value = null;
		switch (bourse) {
			case 1:
				value = getValue(Constant.Dict.HUOBI_ORDER_MIN_VALUE_FLOAT);
				break;
			case 2:
				value = getValue(Constant.Dict.BINANCE_ORDER_MIN_VALUE_FLOAT);
				break;
			case 3:
				value = getValue(Constant.Dict.OKEX_ORDER_MIN_VALUE_FLOAT);
				break;
			case 4:
				//TODO 未配置
				value = getValue(Constant.Dict.OKEX_ORDER_MIN_VALUE_FLOAT);
				break;
			default:
				log.error("未知的交易所标识：" + bourse );
				throw new TransactionException("交易所有误");
		}

		if (null == value) {
			throw new TransactionException("请配置个大交易所下单最金额最小值与浮动比例");
		}
		String[] array = value.split(",");
		map.put("minAmount", new BigDecimal(array[0]));
		map.put("floatRate", new BigDecimal(array[1]));

		return map;
	}
}
